//
//  LiuHomeRetweetPicView.swift
//  LiuWeiBo
//
//  Created by 刘晓晨 on 2017/3/31.
//  Copyright © 2017年 刘晓晨. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

//每一项的间距
private let itemMargin:CGFloat = 5.0

//高度
private let itemHeight = itemWidth

//每一项的宽
private let itemWidth = (screenWidth - 2 * itemMargin - 2 * homeMargin)/3

class LiuHomeRetweetPicView: UICollectionView {

    //懒加载控件
//    fileprivate lazy var picCountLabel:UILabel = UILabel()
    
    //接收图片
    var  picArray:[LiuHomeReweetPicStatuse]? {
        didSet {
            //根据图片的张数修改约束
//            picCountLabel.text = "\((picArray?.count)!)"
            
            updateSzie(count: (picArray?.count)!)
            
            reloadData()
            
        }
        
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowlayout = UICollectionViewFlowLayout()
        flowlayout.itemSize = CGSize(width: itemHeight, height: itemWidth)
        flowlayout.minimumLineSpacing = itemMargin
        flowlayout.minimumInteritemSpacing = itemMargin

        super.init(frame: frame, collectionViewLayout: flowlayout)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //布局UI
    fileprivate func setupUI() {
        
//        addSubview(picCountLabel)
        
//        picCountLabel.snp.makeConstraints { (make) in
//            make.center.equalToSuperview()
//        }
        
        //设置代理
        self.dataSource = self
        
        //cell注册
        register(LiuHomePicCell.self, forCellWithReuseIdentifier: retweetPicId)
    }
    
    //根据图片张数修改约束
    fileprivate func updateSzie(count:Int) {
        
        
        //获取layout
        let layout:UICollectionViewFlowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        
        //判断图片张数
        if count == 1 {
            //获取图片url
            let  url = picArray?.first?.thumbnail_pic
            
            //获取图片
            guard let image = SDWebImageManager.shared().imageCache?.imageFromDiskCache(forKey: url) else {
                return
            }
            
            //图片的大小
            var imageSize = image.size
            
             if image.size.width <= 80 {
                
                //修改显示的大小
                imageSize.width = 80
                imageSize.height = imageSize.height * 80 / imageSize.width
                
                if imageSize.height > 150 {
                    imageSize.height = 150
                }
            }
            
            //更新约束
            self.snp.updateConstraints({ (make) in
                make.size.equalTo(CGSize(width: imageSize.width, height: imageSize.height))
            })
            
            //同步刷新cell的大小
            layout.itemSize = imageSize
            
            return
            
        }
        
        //计算列数
        let cols = count > 3 ? 3 : count
        
        //计算行数
        let rows = (count - 1)/3 + 1
        
        //计算picView的宽高
        let picViewWidth = CGFloat(cols) * itemWidth + itemMargin * CGFloat(cols - 1)
        let picViewHeight = CGFloat(rows) * itemHeight + itemMargin * CGFloat(rows - 1)
        
        //更新约束
        self.snp.updateConstraints { (make) in
            make.size.equalTo(CGSize(width: picViewWidth, height: picViewHeight))
        }
        
        //将cell的大小调整回来
        layout.itemSize = CGSize(width: itemWidth, height: itemHeight)
        
    }

}

extension LiuHomeRetweetPicView:UICollectionViewDataSource {
    
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return picArray?.count ?? 0
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell:LiuHomePicCell = collectionView.dequeueReusableCell(withReuseIdentifier: retweetPicId, for: indexPath) as! LiuHomePicCell
    
            let picStr:LiuHomeReweetPicStatuse = picArray![indexPath.item]
    
    
            cell.picStr = picStr
            
            return cell
        }
}







